<?php

//=================================================IMPORT PACKAGES=========================================
require_once 'private/config/app_system.php';
require_once 'private/config/app_config.php';
require_once 'private/config/app_constant.php';
require_once 'private/config/app_function.php';
set_include_path(INCLUDE_PACKAGE);

//ERROR ==================================================================================================
set_error(ERROR_DISPLAY);

//=================================================Init parameters=========================================
$language = ( isset($_GET["_LANGUAGE"])  && $_GET["_LANGUAGE"] != "" ? $_GET["_LANGUAGE"] : DEFAULT_LANGUAGE);
$language_support = explode(",",LANGUAGE_SUPPORT);
if( !in_array($language,$language_support)){
    redirect(APPLICATION_URL . "/" . $language_support[0] . "/".DEFAULT_CONTROLLER."/".DEFAULT_CONTROLLER_ACTION);
}

$controller = isset($_GET["_CONTROLLER"]) ? $_GET["_CONTROLLER"] : DEFAULT_CONTROLLER;
$controller_action = isset($_GET["_ACTION"]) ? $_GET["_ACTION"] : DEFAULT_CONTROLLER_ACTION;

//=============CONFIG SMARTY ======================//
$smarty = new Smarty();
$smarty->left_delimiter = '<{';
$smarty->right_delimiter = '}>';
$smarty->caching = false;
$smarty->config_overwrite = true;
$smarty_owner = SmartyOwner::getInstance($smarty);

//=================================================CONNECT================================================
RTO::connect(new RDOProxy(RENZA_DBMS, RENZA_HOST_DBMS, RENZA_SUPPLIER_DATABASE_DBMS, RENZA_USERNAME_DBMS, RENZA_PASSWORD_DBMS));

//=================================================START PROCESS==========================================
session_start();

//===============================================RUN MAIN - CALL CONTROLLER SCRIPT========================

if ($controller != "") {

    if(isset($_GET["_IS_AJAX"])){
        $class_controller = ClassHelper::convertClass($controller) . DEFAULT_AJAX_CLASS_SUFFIX;
    }else if(isset($_GET["_IS_SERVICE"])){
        $class_controller = ClassHelper::convertClass($controller) . DEFAULT_SERVICE_CLASS_SUFFIX;
    }else{
        $class_controller = ClassHelper::convertClass($controller) . DEFAULT_CONTROLLER_CLASS_SUFFIX;
    }

    $action_controller = ClassHelper::convertMethod($controller_action);
    
    if (class_exists($class_controller)) {
        try {
            global $_LAYOUTS;
            
            $http_request = new HttpRequest($class_controller
                            , $action_controller
                            , $_POST
                            , $_GET
                            , $_SESSION
                            , $_FILES
                            , $_COOKIE
                            , $smarty_owner
            );

            $http_response = new HttpResponse();
            $class_reflection = new ReflectionClass($class_controller);
            $obj_controller = $class_reflection->newInstance();
            $obj_controller->doAction($http_request, $http_response);
        } catch (Exception $ex) {
            //"An error occurred while processing";
            error_log("+ Date: " . date('d-m-Y h:i:s') . " +\n"
                    . "Url: /$controller/$controller_action \n"
                    . $ex->getMessage() . "\n\n"
                    , 3, ERROR_LOG_DIR);

            //redirect to page fault
            redirect("http://google.com");
        }
    } else {
        //Page is not valid
        if(!isset($_GET["_IS_AJAX"]) && !isset($_GET["_IS_SERVICE"])){
            redirect(APPLICATION_URL . "/" . $language . "/".DEFAULT_CONTROLLER."/".DEFAULT_CONTROLLER_ACTION);
        }
    }
}